<?php

namespace App\Http\Controllers\Admin\SuperAdmin;

use App\Exceptions\LoginException;
use App\Exceptions\SuperAdminException;
use App\Services\Admin\SuperAdmin\User\UserService;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Validation\Rule;

class UserController extends BaseSuperAdminController
{
    private UserService $userService;

    /**
     * @throws Exception
     */
    public function __construct()
    {
        $this->userService = loadService('Admin\SuperAdmin\User\UserService');
        parent::__construct();
    }

    /**
     * 用户详情
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function userInfo(): JsonResponse
    {
        $userInfo = $this->userService->userInfo();
        return outputJsonResult($userInfo);
    }

    /**
     * 用户列表
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function userList(): JsonResponse
    {
        $userList = $this->userService->userList($this->params);
        return outputJsonResult($userList);
    }

    /**
     * 设置用户角色
     *
     * @return JsonResponse
     */
    public function setUserAuthorities(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer',
            'authorityIds' => 'array',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->userService->setUserAuthorities($this->params);
        return outputJsonResult();
    }

    /**
     * 重置密码
     *
     * @return JsonResponse
     * @throws LoginException
     */
    public function resetPassword(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->userService->resetPassword($this->params);
        return outputJsonResult();
    }

    /**
     * 删除用户
     *
     * @return JsonResponse
     * @throws LoginException
     */
    public function deleteUser(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->userService->deleteUser($this->params);
        return outputJsonResult();
    }

    /**
     * 设置自身信息
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function setSelfInfo(): JsonResponse
    {
        $this->userService->setSelfInfo($this->params);
        return outputJsonResult();
    }

    /**
     * 修改密码
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function changePassword(): JsonResponse
    {
        $rule = [
            'oldPassword' => 'required|string|min:5',
            'password' => 'required|string|min:5',
            'qrPassword' => 'required|string|min:5',
        ];
        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->userService->changePassword($this->params);
        return outputJsonResult();
    }

    /**
     * 创建用户
     *
     * @return JsonResponse
     * @throws LoginException
     */
    public function createUser(): JsonResponse
    {
        $rule = [
            'username' => 'required|string:max:128',
            'password' => 'required|string|max:128',
            'nickname' => 'required|string|max:128',
            'enable' => [
                Rule::in([0, 1]),
            ],
            'phone' => 'size:11',
            'email' => 'max:128',
            'authorityId' => 'integer|min:1',
            'headerImg' => 'max:128',
        ];
        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->userService->createUser($this->params);
        return outputJsonResult();
    }

    /**
     * 设置用户信息
     *
     * @return JsonResponse
     */
    public function setUserInfo(): JsonResponse
    {
        $rule = [
            'nickname' => 'string|max:128',
            'enable' => [
                Rule::in([0, 1]),
            ],
            'phone' => 'size:11',
            'email' => 'nullable|max:128',
            'authorityId' => 'integer|min:1',
            'headerImg' => 'nullable|max:128',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->userService->setUserInfo($this->params);
        return outputJsonResult();
    }

    /**
     * 设置用户角色
     *
     * @return JsonResponse
     * @throws SuperAdminException
     */
    public function setUserAuthority(): JsonResponse
    {
        $rule = [
            'id' => 'required|integer',
        ];

        if ($this->ruleCheck($rule)) {
            return outputJsonError();
        }

        $this->userService->setUserAuthority($this->params);
        return outputJsonResult();
    }
}
